package com.sky.zero.common.generate.core.base;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.zero.common.generate.function.ClassField;
import com.sky.zero.enums.SysEnum;
import com.sky.zero.exception.BIZException;
import com.sky.zero.utils.AmountUtil;
import com.sky.zero.utils.JsonUtil;
import com.sky.zero.utils.TreeUtil;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public abstract class BaseService extends Results {

    /**
     * 标记 0否
     **/
    protected static final Integer FLAG_0 = 0;

    /**
     * 标记 1是
     **/
    protected static final Integer FLAG_1 = 1;

    /**
     * 标记 2
     **/
    protected static final Integer FLAG_2 = 2;

    protected static final Integer INSERT_SIZE = 1000;

    protected <T> PageInfo<T> toPageInfo(List<T> list) {
        PageHelper.clearPage();
        return new PageInfo<>(list);
    }

    protected static <T, S> PageInfo<T> toPageInfo(List<S> list, Class<T> targetType) {
        PageInfo<S> oldPage = new PageInfo<>(list);
        PageInfo<T> newPage = new PageInfo<>();
        BeanUtils.copyProperties(oldPage, newPage);
        newPage.setList(copyToList(oldPage.getList(), targetType));
        PageHelper.clearPage();
        return newPage;
    }

    protected static <T, S> PageInfo<T> toPageInfo(PageInfo<S> page, Class<T> targetType) {
        PageInfo<T> newPage = new PageInfo<>();
        BeanUtils.copyProperties(page, newPage);
        newPage.setList(copyToList(page.getList(), targetType));
        PageHelper.clearPage();
        return newPage;
    }

    protected static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType) {
        return BeanUtil.copyToList(collection, targetType, CopyOptions.create());
    }

    protected boolean isNull(String str) {
        return StrUtil.isEmpty(str);
    }

    protected boolean isBlank(String str) {
        return StrUtil.isBlank(str);
    }


    /**
     * 如果存在(true) 则抛异常
     **/
    protected void hasExist(boolean example, SysEnum sysEnum) {
        if (example) throw new BIZException(sysEnum);
    }

    /**
     * 如果对象为空 则抛异常
     **/
    protected void isNull(Object obj, SysEnum sysEnum) {
        hasExist(Objects.isNull(obj), sysEnum);
    }

    /**
     * 如果对象为空 则抛异常
     **/
    protected void isParamNulls(Object... obj) {
        hasExist(Objects.isNull(obj), SysEnum.ERROR1002);
        for (Object o : obj) {
            hasExist(Objects.isNull(o), SysEnum.ERROR1002);
        }
    }

    /**
     * 如果对象不为空 则抛异常
     **/
    protected void isNotNull(Object obj, SysEnum sysEnum) {
        hasExist(Objects.nonNull(obj), sysEnum);
    }

    /**
     * 如果存在(true) 则抛异常
     **/
    protected void hasExist(boolean example, BIZException bizException) {
        if (example) throw bizException;
    }

    /**
     * 如果存在(true) 则抛异常
     **/
    protected void hasExist(boolean example, String code, String msg) {
        if (example) throw new BIZException(code, msg);

    }

    /**
     * 如果存在(true) 则抛异常
     **/
    protected void hasExist(boolean example, Integer code, String msg) {
        if (example) throw new BIZException(code, msg);
    }


    /**
     * 如果存在(true) 则抛异常
     **/
    protected void hasExist(boolean example, String msg) {
        if (example) throw new BIZException(msg);
    }

    /**
     * 如果不存在(false) 则抛异常
     **/
    protected void hasNotExist(boolean example, String code, String msg) {
        if (!example) throw new BIZException(code, msg);
    }

    /**
     * 如果不存在(false) 则抛异常
     **/
    protected void hasNotExist(boolean example, Integer code, String msg) {
        if (!example) throw new BIZException(code, msg);
    }

    /**
     * 如果存在(true) 则抛异常
     **/
    protected void hasExist(boolean example, int row, SysEnum sysEnum) {
        if (example) {
            String msg = String.format("第%s行, %s", row, sysEnum.getMsg());
            throw new BIZException(sysEnum.getCode(), msg);
        }
    }

    /**
     * 如果不存在(false) 则抛异常
     **/
    protected void hasNotExist(boolean example, SysEnum sysEnum) {
        if (!example) throw new BIZException(sysEnum);
    }

    /**
     * 如果不存在(false) 则抛异常
     **/
    protected void hasNotExist(boolean example, String msg) {
        if (!example) throw new BIZException(SysEnum.ERROR500.getCode(), msg);
    }

    protected static <E, P> List<E> listToTree(List<E> list, P parentId, Class<E> clazz, ClassField<E> id, ClassField<E> pid, ClassField<E> children) {
        return TreeUtil.listToTree(list, parentId, clazz, id, pid, children);
    }

    protected <T> T toBean(String json, Class<T> clz) {
        return JsonUtil.toBean(json, clz);
    }

    protected String toJson(Object obj) {
        return JsonUtil.toJson(obj);
    }

    protected <T> Optional<T> of(T value) {
        return Optional.of(value);
    }

    protected <T> Optional<T> ofNullable(T value) {
        return Optional.ofNullable(value);
    }

    public static boolean gt(BigDecimal v1, BigDecimal v2) {
        return AmountUtil.gt(v1, v2);
    }

    public static boolean lt(BigDecimal v1, BigDecimal v2) {
        return AmountUtil.lt(v1, v2);
    }

    public static boolean eq(BigDecimal v1, BigDecimal v2) {
        return AmountUtil.eq(v1, v2);
    }

    public static boolean gteq(BigDecimal v1, BigDecimal v2) {
        return AmountUtil.gteq(v1, v2);
    }

    public static boolean lteq(BigDecimal v1, BigDecimal v2) {
        return AmountUtil.lteq(v1, v2);
    }


}
